﻿using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 整型范结构
/// </summary>
public struct RangeInt
{
    public int min { get; private set; }
    public int max { get; private set; }

    /// <summary>
    /// 返回[min, max)内的随机值
    /// </summary>
    public int random => UnityEngine.Random.Range(min, max);

    /// <summary>
    /// 返回最大值最小值之间的差值
    /// </summary>
    public int difference => max - min;

    public RangeInt(int min, int max)
    {
        this.min = max >= min ? min : max;
        this.max = max >= min ? max : min;
    }

    /// <summary>
    /// 返回[min, max)内的任意数量随机值，并生成一个新数组
    /// </summary>
    public List<int> RandomNumbers(int count)
    {
        var list = new List<int>();
        var size = max - min;
        if (count <= 0)
        {
            return list;
        }
        if ((count << 1) > size)
        {
            for (int i = 0; i < size; i++)
            {
                list.Add(i);
            }
            while (list.Count > count && list.Count > 0)
            {
                list.RemoveAt(Random.Range(0, list.Count));
            }
            return list;
        }
        else
        {
            while (list.Count < count)
            {
                var rand = Random.Range(min, max);
                if (!list.Contains(rand))
                {
                    list.Add(rand);
                }
            }
            return list;
        }
    }

    /// <summary>
    /// 收束取值范围
    /// </summary>
    public int Clamp(int value)
    {
        return Mathf.Clamp(value, min, max);
    }
}